const express = require('express');
const fs = require('fs');
const path = require('path');

const app = express();
const PORT = 3000;
const MOCK_DIR = path.join(__dirname, 'mock');

// 动态加载 JSON 文件并生成路由
fs.readdirSync(MOCK_DIR).forEach(file => {
  if (file.endsWith('.json')) {
    const routeName = `/mock/${file.replace('.json', '')}`;
    const filePath = path.join(MOCK_DIR, file);

    app.get(routeName, (req, res) => {
      const data = require(filePath);
      res.json(data);
    });

    console.log(`Route created: ${routeName}`);
  }
});

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});